<?php
$featureName = '活动';
if(old('combo_id')){
    $combo_id = old('combo_id');
    $combo = \App\Http\Controllers\Admin\Business\Repository\Combo::find($combo_id);
}
?>
@extends('admin.formLayout')
@if(isset($id))
    @section('title','编辑'.$featureName)
@section('barName','编辑'.$featureName)
@else
    @section('title','添加'.$featureName)
@section('barName','添加'.$featureName)
@endif
@section('content')
    <form class="layui-form" @if(!isset($id)) action="{{route($routePrefix.'.add')}}" @else action="{{route($routePrefix.'.edit',['id'=>$id])}}" @endif method="post">
        <div class="layui-tab" lay-filter="posts">
            <ul class="layui-tab-title">
                <li class="layui-this">基本信息</li>
                <li style="color:red">价格</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <div class="layui-row">
                            <div class="layui-col-md8">
                                {{csrf_field()}}
                                @if(isset($id))
                                    <input type="hidden" name="id" value="{{$id}}">
                                @endif
                                <div class="layui-form-item">
                                    <label class="layui-form-label">{{$featureName}}名称<span style="color:red">*</span></label>
                                    <div class="layui-input-block">
                                        <input type="text" name="title" lay-verify="required"  value="@if(old('title')){{old('title')}}@else{{$model->title or ''}}@endif" autocomplete="off" placeholder="请输入名称" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">活动套餐<span style="color:red">*</span></label>
                                    <div class="layui-input-block">
                                        <ul id="step1container">
                                            @if(old('combo_id'))
                                                <li><p>套餐名称：{{$combo->title}}</p></li>
                                            @elseif(isset($model)&&$model->combo)
                                                <li>
                                                    {{$model->combo->title}}
                                                </li>
                                            @endif

                                        </ul>
                                        <input lay-verify="required" type="hidden" name="combo_id" value="@if(old('combo_id'))old('combo_id')@else{{$model->combo_id or ''}}@endif"/>
                                    </div>
                                    <div class="layui-input-block">
                                        <button class="layui-btn" id="step1" type="button"><i class="layui-icon">&#xe654;</i>选择套餐</button>

                                    </div>
                                </div>
                                <div class="layui-form-item layui-form-text">
                                    <label class="layui-form-label">描述</label>
                                    <div class="layui-input-block">
                                        <textarea name="desc"  placeholder="请输入描述..." class="layui-textarea">@if(old('desc')){{old('desc')}}@else{{$model->desc or ''}}@endif</textarea>
                                    </div>
                                </div>
                                <div class="layui-form-item layui-form-text">
                                    <label class="layui-form-label">详情</label>
                                    <div class="layui-input-block">
                                        <textarea lay-ignore  name="content"  id="editor1"  style="width:414px">@if(old('content')){{old('content')}}@else{{$model->content or ''}}@endif</textarea>
                                    </div>
                                </div>

                            </div>
                            <div class="layui-col-md4">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">状态</label>
                                    <div class="layui-input-block">
                                        @foreach($status as $k=>$v)
                                            <input type="radio" name="status" value="{{$k}}" title="{{$v}}" @if(old('status')) {{$k==old('status')?'checked':''}} @else @if(isset($model->status) && $k==$model->status) checked @endif @endif />
                                        @endforeach
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">排序</label>
                                    <div class="layui-input-block">
                                        <input type="number" name="sort" value="@if(old('sort')){{old('sort')}}@else{{$model->sort or ''}}@endif"  class="layui-input" style="width:50px;">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">封面</label>
                                    <div class="layui-input-block">
                                        @php
                                            if(old('thumb')){
                                                $imgUrl = old('thumb');
                                            }
                                            else{
                                                $imgUrl = isset($model->thumb) ? $model->thumb : '';
                                            }
                                        @endphp
                                        @include('admin.form.image',['name'=>'thumb','value'=>$imgUrl,'msg'=>'封面尺寸为480*360'])
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">活动时间<span style="color:red">*</span></label>
                                    <div class="layui-input-block">
                                        <input type="text" name="activetime" lay-verify="required"  value="@if(old('activetime')){{old('activetime')}}@else{{(isset($model->start_time)&&isset($model->end_time))&&($model->start_time&&$model->end_time)?$model->start_time.'~'.$model->end_time:''}}@endif" autocomplete="off"  class="layui-input" id="activetime">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn" lay-submit="" lay-filter="form">立即提交</button>
                                    <button type="button"  onclick="javascript:location.href='{{route($routePrefix)}}'" class="layui-btn layui-btn-primary">返回</button>
                                </div>
                            </div>
                    </div>
                </div>
                <div class="layui-tab-item">
                    <div class="layui-row">
                        <div class="layui-form-item">
                            <label class="layui-form-label">活动方式</label>
                            <div class="layui-input-block">
                                @foreach($types as $k=>$v)
                                    <input type="radio" name="type" value="{{$k}}" title="{{$v}}" lay-filter="type" @if(old('type')) {{$k==old('type')?'checked':''}} @else @if(isset($model->type) && $k==$model->type) checked @endif @endif />
                                @endforeach
                            </div>
                        </div>
                        <div class="layui-form-item type_cascade" id="rebate">
                            <label class="layui-form-label">折扣（%）</label>
                            <div class="layui-input-block">
                                <input type="text" name="rebate"   value="@if(old('rebate')){{old('rebate')}}@else{{$model->rebate or ''}}@endif"   class="layui-input"  style="width:100px;" step="0.01" min="0" placeholder="0.00">
                            </div>
                        </div>
                        <div class="layui-form-item type_cascade" id="reduce">
                            <label class="layui-form-label">减免(￥)</label>
                            <div class="layui-input-block">
                                <input type="text" name="reduce"   value="@if(old('reduce')){{old('reduce')}}@else{{$model->reduce or ''}}@endif" class="layui-input"  style="width:100px;" step="0.01" min="0" placeholder="0.00"/>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">返利方式</label>
                            <div class="layui-input-block">
                                @foreach($rate_type as $k=>$v)
                                    <input type="radio" name="rate_type" value="{{$k}}" title="{{$v}}" lay-filter="rate" @if(old('rate_type')) {{$k==old('rate_type')?'checked':''}} @else @if(isset($model->rate_type) && $k==$model->rate_type) checked @endif @endif />
                                @endforeach
                            </div>
                        </div>
                        <div class="layui-form-item rate_cascade" id="rate">
                            <label class="layui-form-label">上级利率（%）</label>
                            <div class="layui-input-block">
                                <input type="text" name="rate"   value="@if(old('rate')){{old('rate')}}@else{{$model->rate or ''}}@endif"   class="layui-input"  style="width:100px;" step="0.01" min="0" placeholder="0.00">
                            </div>
                        </div>
                        <div class="layui-form-item rate_cascade" id="top_rate">
                            <label class="layui-form-label">顶级利率(%)</label>
                            <div class="layui-input-block">
                                <input type="text" name="top_rate"   value="@if(old('top_rate')){{old('top_rate')}}@else{{$model->top_rate or ''}}@endif" class="layui-input"  style="width:100px;" step="0.01" min="0" placeholder="0.00"/>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit="" lay-filter="form">立即提交</button>
                                <button type="button"  onclick="javascript:location.href='{{route($routePrefix)}}'" class="layui-btn layui-btn-primary">返回</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </form>
@endsection
@section('script')

    <script src="/js/ueditor/ueditor.config.js"></script>
    <script src="/js/ueditor/ueditor.all.js"></script>

    <script type="text/javascript">
        var ue = UE.getEditor('editor1',{
            initialFrameWidth:414,
            initialFrameHeight:400,
            toolbars: [
                [
                    'anchor', //锚点
                    'undo', //撤销
                    'redo', //重做
                    'bold', //加粗
                    'indent', //首行缩进
                    'snapscreen', //截图
                    'italic', //斜体
                    'underline', //下划线
                    'strikethrough', //删除线
                    'subscript', //下标
                    'fontborder', //字符边框
                    'superscript', //上标
                    'formatmatch', //格式刷
                    'source', //源代码
                    'blockquote', //引用
                    'pasteplain', //纯文本粘贴模式
                    'selectall', //全选
                    'print', //打印
                    'preview', //预览
                    'horizontal', //分隔线
                    'removeformat', //清除格式
                    'time', //时间
                    'date', //日期
                    'unlink', //取消链接
                    'insertrow', //前插入行
                    'insertcol', //前插入列
                    'mergeright', //右合并单元格
                    'mergedown', //下合并单元格
                    'deleterow', //删除行
                    'deletecol', //删除列
                    'splittorows', //拆分成行
                    'splittocols', //拆分成列
                    'splittocells', //完全拆分单元格
                    'deletecaption', //删除表格标题
                    'inserttitle', //插入标题
                    'mergecells', //合并多个单元格
                    'deletetable', //删除表格
                    'cleardoc', //清空文档
                    'insertparagraphbeforetable', //"表格前插入行"
                    'insertcode', //代码语言
                    'fontfamily', //字体
                    'fontsize', //字号
                    'paragraph', //段落格式
                    'simpleupload', //单图上传
                    'insertimage', //多图上传
                    'edittable', //表格属性
                    'edittd', //单元格属性
                    'link', //超链接
                    'emotion', //表情
                    'spechars', //特殊字符
                    'searchreplace', //查询替换
                    'map', //Baidu地图
                    'insertvideo', //视频
                    'help', //帮助
                    'justifyleft', //居左对齐
                    'justifyright', //居右对齐
                    'justifycenter', //居中对齐
                    'justifyjustify', //两端对齐
                    'forecolor', //字体颜色
                    'backcolor', //背景色
                    'insertorderedlist', //有序列表
                    'insertunorderedlist', //无序列表
                    'fullscreen', //全屏
                    'directionalityltr', //从左向右输入
                    'directionalityrtl', //从右向左输入
                    'rowspacingtop', //段前距
                    'rowspacingbottom', //段后距
                    'pagebreak', //分页
                    'insertframe', //插入Iframe
                    'imagenone', //默认
                    'imageleft', //左浮动
                    'imageright', //右浮动
                    'attachment', //附件
                    'imagecenter', //居中
                    'wordimage', //图片转存
                    'lineheight', //行间距
                    'edittip ', //编辑提示
                    'customstyle', //自定义标题
                    'autotypeset', //自动排版
                    'webapp', //百度应用
                    'touppercase', //字母大写
                    'tolowercase', //字母小写
                    'background', //背景
                    'template', //模板
                    'scrawl', //涂鸦
                    'music', //音乐
                    'inserttable', //插入表格
                    'charts', // 图表
                ]
            ]
        });
    </script>
    <script>
        var combo = {};

        layui.use(['element','form','layer','jquery','laydate'], function(){
            var element = layui.element;
            var form = layui.form;
            var layer = layui.layer;
            var $ = layui.jquery;
            var laydate = layui.laydate;
            //选择套餐
            $(document).on('click','#step1',function () {
                layer.open({
                    type: 2,
                    title: '选择套餐',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['90%', '100%'],
                    resize:true,
                    content: '{{route($routePrefix.'.comboList')}}', //iframe的url
                    success:function () {
                        combo = {}
                    },
                    end:function () {
                        if(combo&&combo.id!=undefined){
                            str = '<li>';
                            str +='<p>套餐名称：'+combo.title+'</p>'
                            str +='</li>';
                            $('#step1container').html(str);
                            $('#step1').text('重新选择套餐');
                            $('[name=combo_id]').val(combo.id);
                        }
                    }
                });
            })
            //时间日期
                laydate.render({
                    elem: '#activetime'
                    ,type: 'datetime'
                    ,range: true
                });
            $('.type_cascade').hide();
            var type = $('[name=type]:checked').val();
            var ischeck = $('[name=type]').prop('checked');
            if(type=={{\App\Http\Controllers\Admin\Business\Service\ComboActivities::REBATE}}&&ischeck){
                $('#rebate').show();
            }else if(type=={{\App\Http\Controllers\Admin\Business\Service\ComboActivities::REDUCE}}&&ischeck){
                $('#reduce').show();
            }else{

            }

            form.on('radio(type)', function(data){
                //console.log(data.elem); //得到checkbox原始DOM对象
//                console.log(data.elem.checked); //是否被选中，true或者false
//                console.log(data.value); //复选框value值，也可以通过data.elem.value得到
                var value   =  data.value;
                var checked = data.elem.checked;
                if(value=={{\App\Http\Controllers\Admin\Business\Service\ComboActivities::REBATE}}&&checked){
                    $('.type_cascade').hide();
                    $('#rebate').show();
                }else if(value=={{\App\Http\Controllers\Admin\Business\Service\ComboActivities::REDUCE}}&&checked){
                    $('.type_cascade').hide();
                    $('#reduce').show();
                }
                //console.log(data.othis); //得到美化后的DOM对象
            });
            var rate_value = $('[name=rate_type]:checked').val();
            if(rate_value=={{\App\Http\Controllers\Admin\Business\Service\Combo::RATE_TYPE_USER}}){
                $('.rate_cascade').show();
            }else{

                $('.rate_cascade').hide();
            }
            form.on('radio(rate)', function(data){
                //console.log(data.elem); //得到checkbox原始DOM对象
//                console.log(data.elem.checked); //是否被选中，true或者false
//                console.log(data.value); //复选框value值，也可以通过data.elem.value得到
                var value   =  data.value;
                var checked = data.elem.checked;
                if(value==2&&checked){
                    $('.rate_cascade').show();
                }else{

                    $('.rate_cascade').hide();
                }
                //console.log(data.othis); //得到美化后的DOM对象
            });

            form.on('submit(*)',function(data){
                console.log(data);
                return false;
            });
            //一些事件监听
            element.on('tab(posts)', function(data){
            });
            @if (count($errors) > 0)
                @foreach ($errors->all() as $error)
                    layer.msg('{{$error}}',{'icon':2});
                    @break
                @endforeach
            @endif

        });
    </script>
@endsection